package content

import (
	"fmt"
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/models/db"
)

// 文章分类字段表
type ArticleCategory struct {
	*structs.Common `gorm:"-" json:"-"`
	CategoryId      uint64 `gorm:"primary_key;AUTO_INCREMENT;column:category_id;"` // 自增主键
	Name            string `gorm:"column:name;size:255;"`                          // 分类名称
	Sort            uint   `gorm:"column:sort;size:255;"`                          // 排序方式(数字越小越靠前)
	db.Basics
}

// 表名
func (s *ArticleCategory) TableName() string {
	return "woo_article_category"
}

// 初始化
func NewArticleCategory(com *structs.Common) *ArticleCategory {
	d := &ArticleCategory{}
	return d
}

func (s *ArticleCategory) GetConfig() structs.CFConfig {
	list := structs.CFConfig{}
	list.Column = s.Column()
	list.Forms = s.Forms()
	return list
}

func (s *ArticleCategory) Column() []structs.ColumnCommon {
	result := make([]structs.ColumnCommon, 0)
	result = append(result, structs.ColumnCommon{Title: "ID", DataIndex: "CategoryId"})
	result = append(result, structs.ColumnCommon{Title: "名称", DataIndex: "Name"})
	result = append(result, structs.ColumnCommon{Title: "排序", DataIndex: "Sort"})
	result = append(result, structs.ColumnCommon{Title: "更新时间", DataIndex: "UpdateDate"})
	result = append(result, structs.ColumnCommon{Title: "操作", DataIndex: "Operation"})
	return result
}

func (s *ArticleCategory) Forms() []structs.FormsCommon {
	result := make([]structs.FormsCommon, 0)
	result = append(result, structs.FormsCommon{Key: "CategoryId", Label: "主键ID", Type: "hidden", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Name", Label: "名称", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Sort", Label: "排序", Type: "number", Options: []string{}})
	return result
}

// 创建
func (s *ArticleCategory) Create() error {
	return utils.GORM.Debug().Table(s.TableName()).Create(s).Error
}

// 批量创建
func (s *ArticleCategory) BatchCreate(data []*ArticleCategory) error {
	return utils.GORM.Debug().Table(s.TableName()).Create(data).Error
}

// 获取分页列表及总数
func (s *ArticleCategory) GetListAndTotal(search string, page int, pageSize int) ([]*ArticleCategory, int64) {
	list := make([]*ArticleCategory, 0)
	var total int64 = 0
	// 查询
	query := utils.GORM.Debug().Table(s.TableName())
	query = query.Order("sort asc")
	if len(search) > 0 {
		search = fmt.Sprintf("%%%s%%", search)
		query = query.Where("name = ?", search)
	}

	// 总数
	query.Count(&total)

	// 列表
	offset := (page - 1) * pageSize
	query.Preload("Image").Limit(pageSize).Offset(offset).Find(&list)

	return list, total
}

// 获取单数据
func (s *ArticleCategory) GetFirst(where string, field int) *ArticleCategory {

	// 查询
	query := utils.GORM.Debug().Table(s.TableName())

	query = query.Where(where, field)
	query.Preload("Image").First(&s)
	return s
}

// 更新文章分类
func (s *ArticleCategory) Save() error {
	// 更新
	return utils.GORM.Debug().Table(s.TableName()).Save(s).Error
}

// 删除文章分类
func (s *ArticleCategory) Delete() error {
	return utils.GORM.Debug().Table(s.TableName()).Delete(s).Error
}

func SetOption(List []ArticleCategory) []structs.Options {
	var result []structs.Options
	for _, v := range List {
		var option structs.Options
		option.Value = v.CategoryId
		option.Label = v.Name
		result = append(result, option)
	}
	return result
}

// 获取所有文章分类
func GetAllCategory() []structs.Options {
	list := make([]ArticleCategory, 0)

	// 查询
	query := utils.GORM.Debug().Table("woo_article_category")
	query = query.Order("sort asc")
	// 列表
	query.Find(&list)
	result := SetOption(list)
	return result
}

// 获取单条数据
func (s *ArticleCategory) First() error {
	return utils.GORM.Debug().Table(s.TableName()).First(s).Error
}
